﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataBase.Daihoctuonglai;
using System.Data;
using Dhtl_Repositry.UnitOfWork;
using Dhtl_Repositry.Repository;

namespace Dhtl_Repositry.Repository
{
    public class EmailDangkyRepository : RepositoryBase<EmailDangky>
    {
        public EmailDangkyRepository()
            : base()
        {
        }

        public EmailDangkyRepository(WorkUnit unit)
            : base(unit)
        {
        }

        public override int CreateOrUpdate(EmailDangky entity)
        {
            try
            {
                Context.Entry(entity).State = entity.id == 0 ?
                                       EntityState.Added :
                                       EntityState.Modified;
                Context.SaveChanges();
                return entity.id;
            }
            catch { return 0; }
        }

        public override string Delete(int entityId)
        {
            try
            {
                var entity = Context.EmailDangkies.SingleOrDefault(o => o.id == entityId);
                Context.EmailDangkies.Remove(entity);
                Context.SaveChanges();
                return "success";
            }
            catch { return "error"; }
        }

        public override EmailDangky GetById(int entityId)
        {
            try
            {
                return Context.EmailDangkies.SingleOrDefault(p => p.id == entityId);
            }
            catch { return null; }
        }

        public override List<EmailDangky> List()
        {
            try
            {
                return Context.EmailDangkies.OrderByDescending(p => p.DateCreate).ToList();
            }
            catch { return null; }
        }

        public override List<EmailDangky> GetTableListById(int entityId)
        {
            throw new NotImplementedException();
        }

        public EmailDangky GetByEmail(string entityId)
        {
            try
            {
                return Context.EmailDangkies.SingleOrDefault(p => p.Email == entityId);
            }
            catch { return null; }
        }
    }
}
